<?php
Class Menus extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
		$this->data['controller'] = 'menus';
        $this->load->model('Mmenus');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 4, $this->session->userdata('admin_id'));
        
    }
    
    public function index()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mmenus->getMenusDB($config['per_page'],$offset);
        //echo "<pre>"; die(print_r($data));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        //$config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data;
        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        //die('--');
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mmenus->deleteMenusDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
            }
        }
        
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
	private function remove_null_tags($st){
		$Char 	= '<p> </p>|<p>&nbsp;</p>|<p></p>|<span> </span>|<span>&nbsp;</span>|<span></span>|<div> </div>|<div>&nbsp;</div>|<div></div>';
		$explChar 	= explode("|", $Char);
		return str_replace($explChar, '', $st);
	}
	
    public function addmenus()
    {
	
		if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
	
        $this->load->library('form_validation');
		$this->form_validation->set_rules('count_slides', 'Số thực đơn', 'required');  
		$this->form_validation->set_rules('restaurant_id', 'Tên nhà hàng', 'required');
		$this->form_validation->set_rules('zone_id', 'Tên khu vực', 'required');
        //$this->form_validation->set_rules('slides_uploads', 'Hình ảnh', 'required');
        $this->form_validation->set_rules('url_slides_id_1', 'Nội dung thực đơn', 'required');
        $this->data['optionRestaurant'] = $this->Mmenus->optionRestaurant('');
        $this->data['optionZone'] = $this->Mmenus->optionZone('');
        if($this->form_validation->run() === TRUE)
        {
            $count_slides = $this->getParamInt('count_slides');
			$restaurant_id = $this->getParamInt('restaurant_id');
			$zone_id = $this->getParamInt('zone_id');
            $path = 'images/menus/';
            @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
            $arr_upload = array();
			//die('--$count_slides: '.$count_slides.'--$restaurant_id: '.$restaurant_id.'--zone_id: '.$zone_id.'--$path: '.$path);
            for($i = 1;$i <= $count_slides; $i++)
            {
                $data_image = '';
                $dataimagecheck = $this->getParamString('slides_check_id_'.$i);
               // $verify = $this->getParamString('slides_verify_id_'.$i);
               // if(!in_array($verify, array(null, '')))
               // {
                //   if($verify != $dataimagecheck)
                //    {
                   //     @unlink(PROJECT_PATH.'/data/'.$verify);
                   // }
               // }
               //die('')
                $arr_upload[$i]['id'] = $i;
                $arr_upload[$i]['name'] = $this->remove_null_tags($this->getParamString('url_slides_id_'.$i));
                $arr_upload[$i]['name_menus'] = $this->getParamString('name_slides_id_'.$i) ? $this->remove_null_tags($this->getParamString('name_slides_id_'.$i)) : 'Đang cập nhật';
                if($dataimagecheck != 1){
                    $arr_upload[$i]['image'] = $dataimagecheck;
                }else{
                    /*$dataimage = $this->getParamString('slides_id_'.$i);
                    if(!in_array($dataimage, array(null, '', '0')))
                    {
                        $timenow = time().rand(0,9999);
                        $nameimagess = date('Ym').'-menus-'.$timenow.'.jpg';
                        $paths = PROJECT_PATH.'/data/images/menus/';
                        $arr_exp = array('[removed]', '[removed');
                        $exp = str_replace($arr_exp,'',$dataimage);
                        $this->savefile($exp,$paths,$nameimagess);
                        $arr_upload[$i]['image'] = $path.$nameimagess;
                    }else{
                        $arr_upload[$i]['image'] = '';
                    }*/
                    $path1 = 'images/menus/';
        			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
        			$config['upload_path'] = '../data/'.$path1;
        			$config['allowed_types'] = 'gif|jpg|png';
        			$config['max_size']	= '2000';
        			$config['max_width']  = '10240';
        			$config['max_height']  = '7680';
        			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
        			$this->load->library('upload', $config);
        			if( !$this->upload->do_upload('slides_uploads_'.$i)){
        				$arr_upload[$i]['image'] = '';
        			}else{
        				$data_img = $this->upload->data();
        				$arr_upload[$i]['image'] = $path1.$data_img['file_name'];
        			}   
               }
            }
            //die('--<pre>'.print_r($arr_upload));
			//
            $arr = array();
            $arr_n = 0;
            foreach($arr_upload AS $el=>$le)
            {
                if($le['image'] != '')
               {
                   $arr_n = $arr_n + 1;
                   $arr[$arr_n] = $le;
               }
            }
		
            $this->data['data'] = $this->Mmenus->addMenus($restaurant_id,$zone_id,$arr);
			if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/menus/add-menus";</script>');
            }else{
				die('<meta charset="utf-8"><script>alert("Đã tồn tại nhà hàng và khu vực trong hệ thống");window.location.href="'.$this->data['baseurl'].'manage/menus/add-menus";</script>');
			}
        }
		$this->data['action'] = 'addmenus';
        $this->load->view('template/layout', $this->data);
    }
    
    public function ajaxcheckaddmenus(){	
		$zone_id = $this->getParamInt('zone_id');
		$res_id = $this->getParamInt('res_id');
		die(json_encode($this->Mmenus->ajaxCheckAddmenus($zone_id,$res_id)));
	}
    
    public function editmenus()
    {
	
		if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
		
        $this->load->library('form_validation');
		$this->form_validation->set_rules('count_slides', 'Số thực đơn', 'required');  
		$this->form_validation->set_rules('restaurant_id', 'Tên nhà hàng', 'required');
		$this->form_validation->set_rules('zone_id', 'Tên khu vực', 'required');
        //$this->form_validation->set_rules('slides_uploads', 'Hình ảnh', 'required');
        $this->form_validation->set_rules('url_slides_id_1', 'Tên món ăn', 'required');
		$id = $this->getParamUri(4,1);
		$data = $this->Mmenus->getMenusInfo($id);
		$this->data['getMenusInfo'] = $data;
		
        $this->data['optionRestaurant'] = $this->Mmenus->optionRestaurant($data->restaurant_id);
        $this->data['optionZone'] = $this->Mmenus->optionZone($data->zone_id);
        if($this->form_validation->run() === TRUE)
        {
            $count_slides = $this->getParamInt('count_slides');
			$restaurant_id = $this->getParamInt('restaurant_id');
			$zone_id = $this->getParamInt('zone_id');
            $path = 'images/menus/';
            @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
            $arr_upload = array();
			//die('--$count_slides: '.$count_slides.'--$restaurant_id: '.$restaurant_id.'--zone_id: '.$zone_id.'--$path: '.$path);
            for($i = 1;$i <= $count_slides; $i++)
            {
                $data_image = '';
                $dataimagecheck = $this->getParamString('slides_check_id_'.$i);
                $verify = $this->getParamString('slides_verify_id_'.$i);
                if(!in_array($verify, array(null, '')))
                {
                   if($verify != $dataimagecheck)
                    {
                       @unlink(PROJECT_PATH.'/data/'.$verify);
                    }
                }
              
                $arr_upload[$i]['id'] = $i;
                $arr_upload[$i]['name'] = $this->remove_null_tags($this->getParamString('url_slides_id_'.$i));
                $arr_upload[$i]['name_menus'] = $this->getParamString('name_slides_id_'.$i) ? $this->remove_null_tags($this->getParamString('name_slides_id_'.$i)) : 'Đang cập nhật';
                if($dataimagecheck != 1){
                    $arr_upload[$i]['image'] = $dataimagecheck;
                }else{
                    /*$dataimage = $this->getParamString('slides_id_'.$i);
                    if(!in_array($dataimage, array(null, '', '0')))
                    {
                        $timenow = time().rand(0,9999);
                        $nameimagess = date('Ym').'-menus-'.$timenow.'.jpg';
                        $paths = PROJECT_PATH.'/data/images/menus/';
                        $arr_exp = array('[removed]', '[removed');
                        $exp = str_replace($arr_exp,'',$dataimage);
                        $this->savefile($exp,$paths,$nameimagess);
                        $arr_upload[$i]['image'] = $path.$nameimagess;
                    }else{
                        $arr_upload[$i]['image'] = '';
                    } */
                    
                    $path1 = 'images/menus/';
        			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
        			$config['upload_path'] = '../data/'.$path1;
        			$config['allowed_types'] = 'gif|jpg|png';
        			$config['max_size']	= '2000';
        			$config['max_width']  = '10240';
        			$config['max_height']  = '7680';
        			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
        			$this->load->library('upload', $config);
        			if( !$this->upload->do_upload('slides_uploads_'.$i)){
        				$arr_upload[$i]['image'] = '';
        			}else{
        				$data_img = $this->upload->data();
        				$arr_upload[$i]['image'] = $path1.$data_img['file_name'];
        			}     
               }
            }
            //die('--<pre>'.print_r($arr_upload));
			//
            $arr = array();
            $arr_n = 0;
            foreach($arr_upload AS $el=>$le)
            {
                if($le['image'] != '')
               {
                   $arr_n = $arr_n + 1;
                   $arr[$arr_n] = $le;
               }
            }
            $this->data['data'] = $this->Mmenus->editMenus($id,$restaurant_id,$zone_id,$arr);
			if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thành công");window.location.href="'.$this->data['baseurl'].'manage/menus/edit-menus/'.$id.'";</script>');
            }
        }
		$this->data['action'] = 'editmenus';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deletemenus()
    {
	
		if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
		
        $id = $this->getParamUri(4,1);
        $run = $this->Mmenus->deleteMenusDb($id);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/menus/index";</script>');
        }
    }
    
    public function getzone()
    {
        die($this->Mrestaurant->optionRestauRantZone($this->getParamInt('id')));
    }
}